<?php session_start();
      $_SESSION['uid']="";
      //echo $_SERVER['QUERY_STRING'];
      // Parse gadget URL and emit <script src=...</script> statements into the HTML output. 
      // The <script src=...</script> statements will load the libraries passed in via the URL.
      $libraries = split(",", $_GET["libs"]);
      foreach ($libraries as $script) {
        if (preg_match('@^[a-z0-9/._-]+$@i', $script)
          && !preg_match('@([.][.])|([.]/)|(//)@', $script)) {
            print "<script src='http://www.google.com/ig/f/$script'></script>";
        }
      } 
      ?>

<script language="javascript" src="js/jquery.js" type="text/javascript"></script>
<link rel="stylesheet" href="css/flora.datepicker.css" type="text/css" media="screen" title="Flora (Default)">
<script src="js/ui.datepicker.js"></script>
<script>
usrid="";
function todayStr() {
  var today=new Date()
  return (today.getYear() + 1900)+"-"+(today.getMonth()+1)+"-"+today.getDate();
}

var prefs = new _IG_Prefs();
function setUsrid() {  
   usrid = prefs.getInt("userid");
   if(usrid==0){
   <?php 
   $rid=rand(10000,99999)."".time();
   ?>
    prefs.set("userid", <?=$rid?>);
   }
   $("#usr").attr("uid",prefs.getInt("userid"));
   $.get("ajax_request.php?request=SETUSR&usrid="+prefs.getInt("userid"));
}

function showMe(frmID){
      $(".screenForm").css("visibility","hidden");
      $("#"+frmID).css("visibility","visible");  	
}

function attachEvent(){
$(".g_item").click(function() {
	if(itemEdit=="OFF"){
		itemEdit="ON";
		divID=$(this).attr("id");
		divText=$(this).html();
		//console.log(divID);
		$(this).html('<input type="text" size="30" maxlength="30" class="g_item_text" id="ti_'+divID+'" >');
		$("#ti_"+divID).focus();
		$("#ti_"+divID).val(divText);
		$("#ti_"+divID).bind("blur",function(){
			txtVal=($("#ti_"+divID).val());
			$("#"+divID).html($("#ti_"+divID).val());
			updateROW("UPDATE_ITEM",txtVal,divID);
			if(txtVal==""){
				//console.log("DELETE");
				$("#row_"+divID).fadeOut();
				decorateRow();
			}
			itemEdit="OFF";			
		});
	}
});


$(".g_amt").click(function() {
	if(amtEdit=="OFF"){
		amtEdit="ON";
		divID=$(this).attr("id");
		divText=$(this).html();
		divID=divID.split("_")[1];
		//console.log(divID);
		$(this).html('<input align="right" size="6" maxlength="8" type="text" class="g_amt_text" id="ta_'+divID+'" >');
		$("#ta_"+divID).focus();
		$("#ta_"+divID).val(divText);
		$("#ta_"+divID).bind("blur",function(){
			txtVal=$("#ta_"+divID).val();
			$("#a_"+divID).html($("#ta_"+divID).val());
			updateROW("UPDATE_AMT",txtVal,divID);
			amtEdit="OFF";			
		});
	}
});
}



function reportingCall(){
			$.ajax({
			url: 'ajax_request.php',
			data: {'request':'LOAD_REPORT','startDt':$("#startDate").val(),'endDt':$("#endDate").val()},
			success: function(data) {
			$('#g_items_row').html(data);
			getTotal();
			decorateRow();
      //?Not working inside attachEvent Chk this//
     $(".add_new").click(function(){
        rdt=$(this).attr("dt") ;
		addItemRow(rdt);
      }); 
			attachEvent();
		}
	});
}

function addItemRow(rdt){
			$.ajax({
			url: 'ajax_request.php',
			data: {'request':'ADD_ITEM', 'date':rdt},
			success: function(data) {
			//console.log(345);
		  $('#dt_row_'+rdt).append("<div id=\"row_"+data+"\"><div class=\"g_item\" id=\""+data+"\"></div><div class=\"g_amt\" id=\"a_"+data+"\"></div></div>");	
       decorateRow();
        attachEvent();
        $('.g_item_text').css("visibility","visible");
			}
	});
}

function getTotal(){
			$.ajax({
			url: 'ajax_request.php',
			data: {'request':'GET_TOTAL','startDt':$("#startDate").val(),'endDt':$("#endDate").val()},
			success: function(data) {
			$('#total_amt').html(data);
		}
	});
}

function updateROW(req,txt,id){
			$.ajax({
			url: 'ajax_request.php',
			data: {'request':req,'id':id,'txt':txt},
	});
	getTotal();
}

function decorateRow(){
    $('.g_item').removeClass('o_row');
    $('.g_item').removeClass('e_row');
    $('.g_amt').removeClass('e_row');
    $('.g_amt').removeClass('o_row');
		$('.g_item:odd').addClass('o_row');
		$('.g_item:even').addClass('e_row');
		$('.g_amt:odd').addClass('o_row');
		$('.g_amt:even').addClass('e_row');
		$('.g_item_text').css("visibility","hidden");
}

$(document).ready(function() {
    showMe("g_items_row"); 
    addItem="OFF";
    setUsrid();


    $("#startDate").val(todayStr()); 
    $("#endDate").val($("#startDate").val());
    $("#report_date").html($("#startDate").val());
   $("#accountid").val(usrid);
      
   $("#report").click(function(){
      showMe("calendar");
    });

    $("#help").click(function(){
        showMe("helpDoc");
    });
    
   $("#usr").click(function(){
        showMe("setting");
    });
    
    $("#help_ok").click(function(){
        showMe("g_items_row");  
    });
    
    $("#setting_ok").click(function(){
    prefs.set("userid", $("#accountid").val());
    setUsrid() ;
    showMe("g_items_row");  
    });
    
    $("#date_ok").click(function(){
      showMe("g_items_row");     
     if($("#startDate").val()!=$("#endDate").val()){
       $("#report_date").html($("#startDate").val()+" - "+$("#endDate").val());
       }else{
       $("#report_date").html($("#startDate").val());
       }
         reportingCall();         
});
    $("#startDate,#endDate").datepicker({ 
        beforeShow: $.datepicker.customRange, 
        showOn: "both", 
        buttonImage: "images/calendar.gif", 
        buttonImageOnly: true 
    });
itemEdit="OFF";
amtEdit="OFF";
reportingCall();

});
</script>
<style>
#l_c1{
	background-image: url('images/l_c1.gif');
	background-repeat:no-repeat;
	width:20px;
	height:20px;
}
#t_m{
	background-image: url('images/t_m.gif');
	background-repeat:repeat-x;
	height:20px;
}
#r_c1{
	background-image: url('images/r_c1.gif');
	background-repeat:no-repeat;
	height:20px;
	width:20px;
}

#l_s1{
	background-image: url('images/l_s1.gif');
	background-repeat:repeat-y;
}
#h_m{
	background-image: url('images/h_m.gif');
	background-repeat:repeat;
	height:50px;
}
#r_s1{
	background-image: url('images/r_s1.gif');
	background-repeat:repeat-y;
}

#l_s2{
	background-image: url('images/l_s2.gif');
	background-repeat:repeat-y;
}
#m_m2{
	background-image: url('images/m_m2.gif');
	background-repeat:repeat;
}
#r_s2{
	background-image: url('images/r_s2.gif');
	background-repeat:repeat-y;
}
#l_c2{
	background-image: url('images/l_c2.gif');
	background-repeat:no-repeat;
	height:20px;
	width:20px;
}
#b_m2{
	background-image: url('images/b_m2.gif');
	background-repeat:repeat-x;
	height:20px;
}
#r_c2{
	background-image: url('images/r_c2.gif');
	background-repeat:no-repeat;
	height:20px;
}
</style>

<style>
#screen{
	background-color:#FFFFFF;
	margin:15px 0px 10px 0px;
	height:380px;
	width:100%;
}

</style>
<table border="0" cellspacing="0" cellpadding="0" width="300px"  height="450px">
<tr>
	<td id="l_c1" ></td>
	<td id="t_m" ></td>
	<td id="r_c1"></td>
</tr>
<tr>
	<td id="l_s1" ></td>
	<td id="h_m"><h3>Plan Your Study</h3></td>
	<td id="r_s1" ></td>
</tr>
<tr>
	<td id="l_s2" ></td>
	<td id="m_m2">
	<div id="screen">
		Ashish
	</div>
	<!--BODY-->
	</td>
	<td id="r_s2" ></td>
</tr>
<tr>
	<td id="l_s2" ></td>
	<td id="m_m2" align="right"><a href="http://www.eddytools.com/tools/google-gadget.htm" border="0" target="_blank"><img border="0" src="images/eddytools_trans_small.png"></a></td>
	<td id="r_s2" ></td>
</tr>
<tr>
	<td id="l_c2" ></td>
	<td id="b_m2" align="right"></td>
	<td id="r_c2"></td>
</tr>
</table>